#include "CPlayerWalkState.h"
#include "CPlayer.h"
#include "CPlayerIdleState.h"
#include "CPlayerJumpState.h"
#include "CSGD_DirectInput.h"
#include "CTimer.h"

#define GRAVITY 20.0f

CPlayerWalkState::CPlayerWalkState()
{
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(Greek_Walk));
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(MidEvil_Walk));
	m_vAnimations.push_back(CAnimationEngine::GetInstance()->LoadAnimation(Gothic_Walk));
}

CPlayerWalkState *CPlayerWalkState::GetInstance()
{
	static CPlayerWalkState Instance;
	return &Instance;
}

void CPlayerWalkState::Enter(CPlayer *pPlayer)
{
	m_pPlayer = pPlayer;
}

//TODO: Tell Juno about all the normalize calls I am making
void CPlayerWalkState::Run()
{
	m_vAnimations[m_pPlayer->m_unTimePeriod]->Play(CTimer::GetInstance()->GetElapsedTime());

	Vec3 DownVec = -m_pPlayer->m_Pos;
	DownVec.Normalize();

	Vec3 vFormerGravity = Dot(DownVec, m_pPlayer->m_Vel) * DownVec;

	CSGD_DirectInput * di = CSGD_DirectInput::GetInstance();
	if(di->KeyDown(DIK_UPARROW))
	{
		Vec3 Up(m_pPlayer->m_Pos.x, m_pPlayer->m_Pos.y, 0);
		Up.Normalize();
		Up *= m_pPlayer->m_fJumpSpeed;
		m_pPlayer->m_Vel = Up;
		float x = Up.Length();
		m_pPlayer->ChangeState(	CPlayerJumpState::GetInstance());
	}
	if(di->KeyDown(DIK_LEFTARROW))
	{
		m_pPlayer->m_bFlipped = true;

		Vec3 Right(-m_pPlayer->m_Pos.y, m_pPlayer->m_Pos.x, 0);
		Right.Normalize();
		Right *= m_pPlayer->m_fWalkSpeed;

		m_pPlayer->m_Vel = Right + vFormerGravity;
	}
	if(di->KeyDown(DIK_RIGHTARROW))
	{
		m_pPlayer->m_bFlipped = false;

		Vec3 Left(m_pPlayer->m_Pos.y, -m_pPlayer->m_Pos.x, 0);
		Left.Normalize();
		Left *= m_pPlayer->m_fWalkSpeed;

		m_pPlayer->m_Vel = Left + vFormerGravity;
	}
	
	if(di->KeyUp(DIK_UPARROW) && di->KeyUp(DIK_LEFTARROW) && di->KeyUp(DIK_RIGHTARROW))
	{
		m_pPlayer->m_Vel = Vec3(0, 0, 0);
		m_pPlayer->ChangeState(	CPlayerIdleState::GetInstance());
	}
	
	
	DownVec *= GRAVITY;
	m_pPlayer->m_Vel += DownVec * CTimer::GetInstance()->GetElapsedTime();
}

void CPlayerWalkState::Exit()
{
}